package com.icoolkj.learn.juc.base;

import lombok.extern.slf4j.Slf4j;

/**
 * @author icoolkj
 * @version 1.0
 * @description 守护线程
 * @createDate 2025/03/04 13:04
 */
@Slf4j
public class DaemonThreadDemo {

    public static void main(String[] args) {
        log.info("开始运行...");
        Thread t1 = new Thread(() -> {
            log.info("{} 开始执行", Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            log.info("{} 运行结束", Thread.currentThread().getName());
        }, "t1");

        // 设置 t1 为守护线程
        t1.setDaemon(true);

        t1.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        log.info("运行结束...");

    }
}
